package app.texas.com.devilfishhouse.weight;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.universal_library.AppConfig;
import com.universal_library.utils.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import app.texas.com.devilfishhouse.Beans.BannerBean;
import app.texas.com.devilfishhouse.R;


/**
 * Created by solin on 2017/2/6.
 * <p>
 * 自定义的轮播图
 */


public class BannerView extends LinearLayout {
    private Context mcontext;
    private ViewPager mViewPager;
    private LinearLayout ll_points;
    private int mPiction = 0;//记录上一个viewpage 的位置  初始化0
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int currentItem = mViewPager.getCurrentItem();
            currentItem++;
            mViewPager.setCurrentItem(currentItem);
            mHandler.sendEmptyMessageDelayed(0, 5000);
        }
    };
    private MyAdapter adapter;

    public BannerView(Context context) {
        this(context, null);
    }

    public BannerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mcontext = context;
        initView();
    }

    /**
     * 初始化布局
     */
    private void initView() {
        LayoutInflater.from(mcontext).inflate(R.layout.banner_view, this, true);
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        ll_points = (LinearLayout) findViewById(R.id.ll_points);
    }

    /**
     * 设置数据
     * <p>
     * 现在是假数据
     */
    public void setdata(final List<BannerBean> listBanner) {

        initPoint(listBanner);
        //viewpage设置数据
        if (adapter != null) {
//            adapter = new MyAdapter(listBanner);
            adapter.notifyDataSetChanged();
        } else {
            adapter = new MyAdapter(listBanner);
        }
        adapter.notifyDataSetChanged();
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(listBanner.size() * 10000);
        //轮滑监听
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //页面滑动事件偏移量
            }

            @Override
            public void onPageSelected(int position) {
                position = position % listBanner.size();//防止角标越界
                //某个页面别选中
                if (ll_points.getChildAt(position) != null)
                    ll_points.getChildAt(position).setEnabled(true);
                if (ll_points.getChildAt(mPiction) != null)
                    ll_points.getChildAt(mPiction).setEnabled(false);
                mPiction = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //页面华东状态的改变
            }
        });
        if (listBanner.size() > 1) {
            //循环播放图片
            mHandler.sendEmptyMessageDelayed(0, 5000);
        }
        //重画
        invalidate();
    }

    /**
     * 初始化圆点个数
     */
    private void initPoint(List<BannerBean> listBanner) {
        if (ll_points.getChildCount() != listBanner.size()) {
            if (ll_points.getChildCount() > 0)
                ll_points.removeAllViews();
            if (listBanner.size() > 0) {
                for (int i = 0; i < listBanner.size(); i++) {
                    ImageView imageView = new ImageView(mcontext);
                    LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    params.setMargins(5, 0, 5, 0);
                    imageView.setLayoutParams(params);
                    if (i > 0) {
                        imageView.setEnabled(false);
                    }
                    imageView.setImageResource(R.drawable.selector_point);
                    ll_points.addView(imageView);
                }
            }
        }
    }

    /**
     * viewpage的adapter
     */
    class MyAdapter extends PagerAdapter {
        private List<BannerBean> listBanner = new ArrayList<>();

        public MyAdapter(List<BannerBean> listBanner) {
            this.listBanner = listBanner;
        }

        @Override
        public int getCount() {
//            return pics.length;    返回最大值无尽头
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (listBanner.size() > 0)
                position = position % listBanner.size();//取余数 ，防止角标越界

            ImageView iv = new ImageView(mcontext);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            RequestManager manager = Glide.with(mcontext);
            if (listBanner.size() > 0) {
                ImageLoader.loadImage(manager, iv, listBanner.get(position).getUrl());
//                iv.setImageResource(listBanner.get(position));
                final int finalPosition = position;
                iv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                        if (listBanner.get(finalPosition).getBannerUrl().length() > 2) {
////                            AppCheck.showWebView(mcontext, R.string.app_name, listBanner.get(finalPosition).getPicH5Url());
//                        }
                    }
                });
            }
            container.addView(iv);
            return iv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
